﻿Imports DevComponents.DotNetBar
Imports System.Text.RegularExpressions

Public Class ucThongtinnguoidung

    Private userBUS As New UserBUS()
    Private privilegeBUS As New PrivilegesBUS()
    Private dtuseraccount As DataTable

    Private Sub ButtonItemThoat_Click(sender As Object, e As EventArgs) Handles ButtonItemThoat.Click
        Dim uc As New ucThongtinnguoidung
        Me.Controls.Add(uc)
        Dim parent = DirectCast(Application.OpenForms.Item(0), Main)
        parent.CloseModalPanel(uc, DevComponents.DotNetBar.Controls.eSlideSide.Right)
    End Sub

    Private Sub ucThongtincanhan_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        LabelXTieude.ForeColor = Color.White

        'LoadCurrentUser()
        LoadAccountList()
        'LoadAccountInfo()
        LoadPrivilegesList()

    End Sub



    Private Sub ButtonItemEditTendangnhap_Click(sender As Object, e As EventArgs) Handles ButtonItemEditTendangnhap.Click
        ButtonItemEditTendangnhap.Visible = False
        ButtonItemSaveTendangnhap.Visible = True

        LabelItemTendangnhap.Visible = False
        TextBoxItemTendangnhap.Text = LabelItemTendangnhap.Text
        TextBoxItemTendangnhap.Visible = True

        ItemPanelTendangnhap.Refresh()
        TextBoxItemTendangnhap.Focus()
    End Sub

    Private Sub ButtonItemSaveTendangnhap_Click(sender As Object, e As EventArgs) Handles ButtonItemSaveTendangnhap.Click
        If TextBoxItemTendangnhap.Text = "" Then
            ErrorProvider1.SetError(TextBoxItemTendangnhap.TextBox, "Vui lòng nhập họ và tên")
            Exit Sub
        Else
            ErrorProvider1.SetError(TextBoxItemTendangnhap.TextBox, "")
        End If

        ButtonItemEditTendangnhap.Visible = True
        ButtonItemSaveTendangnhap.Visible = False

        LabelItemTendangnhap.Visible = True
        LabelItemTendangnhap.Text = TextBoxItemTendangnhap.Text
        TextBoxItemTendangnhap.Visible = False

        ItemPanelTendangnhap.Refresh()

        If userBUS.UserAccounts_Update_Name(objUser.UserAccountID, TextBoxItemTendangnhap.Text) Then
            BalloonTip1.SetBalloonCaption(ItemPanelTendangnhap, "Thông báo")
            BalloonTip1.SetBalloonText(ItemPanelTendangnhap, "Cập nhật thành công")
        End If

    End Sub

    Private Sub ButtonItemEditHovaten_Click(sender As Object, e As EventArgs) Handles ButtonItemEditHovaten.Click
        ButtonItemEditHovaten.Visible = False
        ButtonItemSaveHovaten.Visible = True

        LabelItemHovaten.Visible = False
        TextBoxItemHovaten.Text = LabelItemHovaten.Text
        TextBoxItemHovaten.Visible = True

        ItemPanelHovaten.Refresh()
        TextBoxItemHovaten.Focus()
    End Sub

    Private Sub ButtonItemSaveHovaten_Click(sender As Object, e As EventArgs) Handles ButtonItemSaveHovaten.Click
        If TextBoxItemHovaten.Text = "" Then
            ErrorProvider1.SetError(TextBoxItemHovaten.TextBox, "Vui lòng nhập họ và tên")
            Exit Sub
        Else
            ErrorProvider1.SetError(TextBoxItemHovaten.TextBox, "")
        End If

        ButtonItemEditHovaten.Visible = True
        ButtonItemSaveHovaten.Visible = False

        LabelItemHovaten.Visible = True
        LabelItemHovaten.Text = TextBoxItemHovaten.Text
        TextBoxItemHovaten.Visible = False

        ItemPanelHovaten.Refresh()

        If userBUS.UserAccounts_Update_FullName(objUser.UserAccountID, TextBoxItemHovaten.Text) Then
            BalloonTip1.SetBalloonCaption(ItemPanelHovaten, "Thông báo")
            BalloonTip1.SetBalloonText(ItemPanelHovaten, "Cập nhật thành công")
        End If
    End Sub

    Private Sub ButtonItemEditEmail_Click(sender As Object, e As EventArgs) Handles ButtonItemEditEmail.Click
        ButtonItemEditEmail.Visible = False
        ButtonItemSaveEmail.Visible = True

        LabelItemEmail.Visible = False
        TextBoxItemEmail.Text = LabelItemEmail.Text
        TextBoxItemEmail.Visible = True

        ItemPanelEmail.Refresh()
        TextBoxItemEmail.Focus()
    End Sub

    Private Sub ButtonItemSaveEmail_Click(sender As Object, e As EventArgs) Handles ButtonItemSaveEmail.Click
        If ValidationEmail(TextBoxItemEmail.Text) Then
            ButtonItemEditEmail.Visible = True
            ButtonItemSaveEmail.Visible = False

            LabelItemEmail.Visible = True
            LabelItemEmail.Text = TextBoxItemEmail.Text
            TextBoxItemEmail.Visible = False

            ItemPanelEmail.Refresh()

            If userBUS.UserAccounts_Update_Email(objUser.UserAccountID, TextBoxItemEmail.Text) Then
                BalloonTip1.SetBalloonCaption(ItemPanelEmail, "Thông báo")
                BalloonTip1.SetBalloonText(ItemPanelEmail, "Cập nhật thành công")
            End If
        End If
    End Sub

    Private Sub ButtonItemEditSDT_Click(sender As Object, e As EventArgs) Handles ButtonItemEditSDT.Click
        ButtonItemEditSDT.Visible = False
        ButtonItemSaveSDT.Visible = True

        LabelItemSDT.Visible = False
        TextBoxItemSDT.Text = LabelItemSDT.Text
        TextBoxItemSDT.Visible = True

        ItemPanelSDT.Refresh()
        TextBoxItemSDT.Focus()
    End Sub

    Private Sub ButtonItemSaveSDT_Click(sender As Object, e As EventArgs) Handles ButtonItemSaveSDT.Click
        If ValidationSDT(TextBoxItemSDT.Text) = False Then
            Exit Sub
        End If
        ButtonItemEditSDT.Visible = True
        ButtonItemSaveSDT.Visible = False

        LabelItemSDT.Visible = True
        LabelItemSDT.Text = TextBoxItemSDT.Text
        TextBoxItemSDT.Visible = False

        ItemPanelSDT.Refresh()

        If userBUS.UserAccounts_Update_PhoneNumber(objUser.UserAccountID, TextBoxItemSDT.Text) Then
            BalloonTip1.SetBalloonCaption(ItemPanelSDT, "Thông báo")
            BalloonTip1.SetBalloonText(ItemPanelSDT, "Cập nhật thành công")
        End If
    End Sub

    Private Sub TextBoxItemEmail_TextChanged(sender As Object, e As EventArgs) Handles TextBoxItemEmail.TextChanged
        If ValidationEmail(TextBoxItemEmail.Text) Then
            ErrorProvider1.SetError(TextBoxItemEmail.TextBox, "")
        Else
            ErrorProvider1.SetError(TextBoxItemEmail.TextBox, "Vui lòng nhập email hợp lệ. Ví dụ: abc@chanhung.net")
        End If
    End Sub

    Private Sub TextBoxItemSDT_TextChanged(sender As Object, e As EventArgs) Handles TextBoxItemSDT.TextChanged
        If ValidationSDT(TextBoxItemSDT.Text) Then
            ErrorProvider1.SetError(TextBoxItemSDT.TextBox, "")
        Else
            ErrorProvider1.SetError(TextBoxItemSDT.TextBox, "Vui lòng nhập số điện thoại hợp lệ.")
        End If
    End Sub

    Private Function ValidationEmail(str As String) As Boolean
        Dim reg_exp As New Regex("^([0-9a-zA-Z]([-\.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$")
        If reg_exp.IsMatch(str) Then
            Return True
        Else
            Return False
        End If
    End Function

    Private Function ValidationSDT(str As String) As Boolean
        Dim reg_exp As New Regex("^[0-9 ]+$")
        If reg_exp.IsMatch(str) Then
            Return True
        Else
            Return False
        End If
    End Function

    Private Sub ButtonItemSave_Click(sender As Object, e As EventArgs) Handles ButtonItemSave.Click
        If TextBoxXTendangnhap.Text = "" Then
            ErrorProvider1.SetError(TextBoxXTendangnhap, "Vui lòng nhập tên đăng nhập")
        Else
            ErrorProvider1.SetError(TextBoxXTendangnhap, "")
        End If

        If TextBoxXHovaten.Text = "" Then
            ErrorProvider1.SetError(TextBoxXHovaten, "Vui lòng nhập họ và tên")
        Else
            ErrorProvider1.SetError(TextBoxXHovaten, "")
        End If

        If TextBoxXEmail.Text = "" Then
            ErrorProvider1.SetError(TextBoxXEmail, "Vui lòng nhập email")
        Else
            ErrorProvider1.SetError(TextBoxXEmail, "")
        End If

        If ComboBoxExQuyen.SelectedIndex < 0 Then
            ErrorProvider1.SetError(ComboBoxExQuyen, "Vui lòng chọn quyền đăng nhập")
        Else
            ErrorProvider1.SetError(ComboBoxExQuyen, "")
        End If
    End Sub

    Private Sub TextBoxXMatkhau_Leave(sender As Object, e As EventArgs) Handles TextBoxXMatkhau.Leave
        If TextBoxXXacnhanmatkhau.Text <> TextBoxXMatkhau.Text Then
            ErrorProvider1.SetError(TextBoxXXacnhanmatkhau, "Vui lòng xác nhận lại mật khẩu")
        Else
            ErrorProvider1.SetError(TextBoxXXacnhanmatkhau, "")
        End If
    End Sub

    Private Sub TextBoxXXacnhanmatkhau_TextChanged(sender As Object, e As EventArgs) Handles TextBoxXXacnhanmatkhau.TextChanged
        If TextBoxXXacnhanmatkhau.Text <> TextBoxXMatkhau.Text Then
            ErrorProvider1.SetError(TextBoxXXacnhanmatkhau, "Vui lòng xác nhận lại mật khẩu")
        Else
            ErrorProvider1.SetError(TextBoxXXacnhanmatkhau, "")
        End If
    End Sub

    Private Sub LoadCurrentUser()
        LabelItemTendangnhap.Text = objUser.UserName
        LabelItemHovaten.Text = objUser.FullName
        LabelItemEmail.Text = objUser.EmailAddress
        LabelItemSDT.Text = objUser.PhoneNumber
    End Sub

    Private Sub LoadAccountList()
        'For i As Integer = 1 To 2
        '    Dim temp As New BubbleButton
        '    temp.Image = BubbleButtonTest.Image
        '    temp.ImageLarge = BubbleButtonTest.ImageLarge
        '    temp.Name = "BubbleButton" & i
        '    temp.TooltipText = "User " & i
        '    AddHandler temp.Click, AddressOf Click_BubbleButton
        '    BubbleBarTabDS.Buttons.AddRange(New DevComponents.DotNetBar.BubbleButton() {temp})
        'Next

        dtuseraccount = userBUS.UserAccounts_List().Tables(0)
        For Each dr As DataRow In dtuseraccount.Rows
            Dim bb As New BubbleButton
            bb.Image = BubbleButtonTest.Image
            bb.ImageLarge = BubbleButtonTest.ImageLarge
            bb.Name = dr.Item(2).ToString()
            bb.TooltipText = dr.Item(3).ToString()
            bb.Tag = dr.Item(0)
            AddHandler bb.Click, AddressOf Click_BubbleButton
        Next
    End Sub

    Private Sub Click_BubbleButton(sender As Object, e As ClickEventArgs)
        Dim bb As BubbleButton = sender
        'MessageBox.Show(m.TooltipText, "Caption", MessageBoxButtons.OK)

    End Sub

    Private Sub LoadPrivilegesList()
        ComboBoxExQuyen.DataSource = privilegeBUS.Privileges_List().Tables(0)
        ComboBoxExQuyen.DisplayMember = "PrivilegeDesc"
        ComboBoxExQuyen.ValueMember = "PrivilegeID"
    End Sub

    Private Sub ComboBoxExQuyen_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBoxExQuyen.SelectedIndexChanged
        Try
            MessageBox.Show(ComboBoxExQuyen.SelectedValue)
        Catch ex As Exception

        End Try
    End Sub

End Class
